<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\User;
use app\admin\model\ConfigData;
use app\admin\model\UserRoseLog;
use app\admin\model\ShareArticle;

class Share extends Api
{
    protected $NeedLogin = []; //存在则验证token

    public function _initialize(){
	    $action = request()->action();
	
		if(in_array($action,$this->NeedLogin)){
			$ignore = array('host','accept','content-length','content-type');
			$headers = array();
			if($this->request->request('token')){
				$headers['token'] = $this->request->request('token');
			}else{
				foreach($_SERVER as $key=>$value){
					if(substr($key, 0, 5)==='HTTP_'){
						$key = substr($key, 5);
						$key = str_replace('_', ' ', $key);
						$key = str_replace(' ', '-', $key);
						$key = strtolower($key);
						if(!in_array($key, $ignore)){
							$headers[$key] = $value;
						}
					}
				}
			}
			$token=$headers['token'];
			if(strlen($token)!=32){
				$this->error('请登录');
			}
			$this->user=User::where(array('token'=>$token))->find();
			if(!$this->user){
				$this->error('请登录');
			}
			User::where(array('id'=>$this->user['id']))->update(array('active_time'=>time()));
		}
	}
	
    public function getImgs($content,$order='ALL'){
        $pattern="/<img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
        preg_match_all($pattern,$content,$match);
        if(isset($match[1])&&!empty($match[1])){
                if($order==='ALL'){
                    return $match[1];
                }
                if(is_numeric($order)&&isset($match[1][$order])){
                        return $match[1][$order];
                }
        }
        return '';
    }
	//生成文章
	public function article_add(){
		$id = $this->request->request('id');
		$title = $this->request->request('title');
		$content = $this->request->request('content');
        if(strlen($title)<5){
			$this->error('请文章标题最少5个字符长度');
        }
        if(strlen($title)>50){
			$this->error('请文章标题最多50个字符长度');
        }
        if(strlen($content)<50){
			$this->error('请最少输入50个字');
        }
        $imgs=$this->getImgs(htmlspecialchars_decode($content),0);
        if($id){
            ShareArticle::where(array('id'=>$id,'user_id'=>$this->user['id']))->save(array('title'=>$title,'img'=>$imgs,'content'=>$content));
        }else{
            ShareArticle::add(array('user_id'=>$this->user['id'],'title'=>$title,'content'=>$content,'status'=>0,'add_time'=>time()));
        }
		$this->success('保存成功');
	}
	public function article_list(){
		$page = $this->request->request('page',1);
		$pagesize = $this->request->request('pagesize',10);
	    $wheresql="status=1 or (status=0 and user_id='".$this->user['id']."')";
	    $list=ShareArticle::where($wheresql)->order('add_time desc')->limit($page*$pagesize-$pagesize,$pagesize)->select();
	    if($list){
    	    foreach($list as $a=>&$b){
    	        if(empty($b['img'])){
    	            $b['img']=ConfigData::where(array('name'=>'share_article_img'))->getField('value');
    	        }
    	        $b['user']=User::where(array('id'=>$b['user_id']))->find();
	            $b['add_time']=date("Y-m-d H:i:s",$b['add_time']);
	        }
	    }else{
	        $list=[];
	    }
		$this->success('成功',['list'=>$list]);
	}
	public function article(){
		$id = $this->request->request('id');
	    ShareArticle::where(array('id'=>$id))->setInc('views',1);
	    $article=ShareArticle::where(array('id'=>$id))->find();
	    if(empty($article['img'])){
    	    $article['img']='https://app.yuxiang123.cn/Public/avatar/logo.png';
    	}
        $article['add_time']=date("Y-m-d H:i:s",$article['add_time']);
        $article['content']=htmlspecialchars_decode($article['content']);
        $article['user']=User::where(array('id'=>$article['user_id']))->find();
		$this->success('成功',['article'=>$article]);
	}
	public function share_tips(){
		$name = $this->request->request('name','');
	    $share_tips=ConfigData::where(array('name'=>$name))->value('value');
		$this->success('成功',['share_tips'=>$share_tips]);
	}
	//我要创作-分享成功-不奖励-被浏览再奖励
	public function share_success(){
		$id = $this->request->request('id');
		$uid = $this->request->request('uid');
		$type = $this->request->request('type');
	    $msg='成功';
	    if($id && $uid && $type){
	        if($type==1){
    	        $today=time()-3600*6;
	            $shate_total=UserRoseLog::where(array('user_id'=>$this->user['id'],'type'=>16,'add_time'=>array('gt',$today)))->sum('rose_money');
		    	if($shate_total<5){
    		        roseandlog($this->user['id'],5,16,'分享文章到朋友圈奖励');
    		        $msg='分享朋友圈，奖励5分';
			    }
	        }
	        if($type==2){
    	        $today=time()-3600*3;
	            $shate_total=UserRoseLog::where(array('user_id'=>$this->user['id'],'type'=>17,'add_time'=>array('gt',$today)))->sum('rose_money');
		    	if($shate_total<3){
    		        roseandlog($this->user['id'],3,17,'分享文章到微信好友奖励');
    		        $msg='分享给好友，奖励3分';
			    }
	        }
	    }
		$this->success($msg);
	}
}